import 'package:base_flutter/base_flutter.dart';
import 'package:ls_work/a_yyt/models/medical_elder_entity.dart';
import 'package:ls_work/net/response/medicine_advice/drug_list_data.dart';
import 'package:ls_work/net/response/medicine_advice/medicine_elder_list_data.dart';
import 'package:ls_work/ui/medicine/choose_medicine_elder_page.dart';
import 'package:ls_work/ui/medicine/choose_medicine_page.dart';
import 'package:ls_work/ui/select_old_page.dart';

import '../../../model/medicine_advice_model.dart';

class AddMedicineHandOverViewModel extends BaseViewModel {
  AddMedicineHandOverViewModel(super.context);


  ///包装数量
  String? packageNum;

  ///最小单位数量
  String? unitNum;

  MedicineElderListData? elderInfo;

  String? reason;

  DrugListData? drugInfo;

  int isTransfer = 0;

  var model = MedicineAdviceModel();

  Future<void> chooseElder() async {
    var elder = await chooseMedicineElder(context);
    if (elder != null) {
      elderInfo = elder;
      notifyListeners();
    }
  }

  Future<void> chooseMedicine() async {
    DrugListData? data = await Go().push(ChooseMedicinePage());
    if (data != null) {
      drugInfo = data;
      notifyListeners();
    }
  }

  String totalNum = "";

  Future<void> calculateReceiveNum() async {
    if (drugInfo == null) {
      return;
    }
    try {
      var number = await model.calculateMinNum(
          drugId: drugInfo!.id, maxNum: packageNum??"0", minNum: unitNum??"0");
      totalNum = number.toString();
      notifyListeners();
    } catch (e) {
      showToast(e.toNetError());
    }
  }

  void submitClick(){
    if(elderInfo==null){
      showToast("请选择长者");
      return;
    }
    if(drugInfo==null){
      showToast("请选择药品");
      return;
    }
    submitHandover();
  }

  Future<void> submitHandover() async {
   showLoadingDialog(msg: '提交中...');
   try{
     await model.submitMedicineHandover(stockId: elderInfo!.id,
         drugId: drugInfo!.id,
         transferQuantity: totalNum,
         transferReason: reason,
         isTransfer:isTransfer);
     hideDialog();
     showToast("提交成功");
     finish(data: true);
   }catch(e){
     hideDialog();
     showToast(e.toNetError());
   }
  }
}